//
//  DZZAddressViewController.m
//  DZZTravel
//
//  Created by xalo on 16/7/15.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZAddressViewController.h"

@interface DZZAddressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *cityArray;
@property (nonatomic,strong)NSMutableArray *cityNameArray;
@property (nonatomic,strong)NSMutableArray *cityFArray;


@property (nonatomic,strong)NSMutableArray *smallArray;

@property (nonatomic,strong)NSMutableDictionary *dicc;

@end

@implementation DZZAddressViewController
-(NSMutableArray *)smallArray{
    if (!_smallArray) {
        _smallArray = [NSMutableArray array];
    }
    return _smallArray;
}

- (NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}

- (NSMutableArray *)cityFArray{
    if (!_cityFArray) {
        _cityFArray = [NSMutableArray array];
    }
    return _cityFArray;
}
- (NSMutableArray *)cityNameArray{
    if (!_cityNameArray) {
        _cityNameArray = [NSMutableArray array];
    }
    return _cityNameArray;
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
    
    [RequestManager fectDataWithUrl:@"http://appapi.yaochufa.com/v2/Position/GetCityList?system=ios&channel=AppStore&longitude=108.90690590045&imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&lang=app&latitude=34.229654187594&machineCode=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&version=5.2.3" parameter:@{} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic2 in dic[@"data"][@"positionCity"]) {
            CityModel *city = [[CityModel alloc]init];
            [city setValuesForKeysWithDictionary:dic2];
            
            [self.cityArray addObject:city];
            
            
        }
        
       self.dicc = [NSMutableDictionary dictionary];
        for (CityModel *city in self.cityArray) {
            NSString *key;
            if ([city.cityName isEqualToString:@"垦丁市"]) {
                
                key = @"K";
            }else{
               key = [city.pinYinName substringToIndex:1].capitalizedString;
            }
            if (![self.dicc.allKeys containsObject:key]) {
            [self.smallArray addObject:key];
                
            NSMutableArray *array = [NSMutableArray array];
                
            [array addObject:city];
                [self.dicc setObject:array forKey:key];
            }else{
                [self.dicc[key] addObject:city];
            }
        }
        [self.smallArray sortUsingSelector:@selector(compare:)];
        
        
    } upDateUI:^{
        [self.tableView reloadData];
        
        
    }];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.smallArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dicc[self.smallArray[section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = [self.dicc[self.smallArray[indexPath.section]][indexPath.row]cityNameAbbr];
    cell.backgroundColor =[UIColor yellowColor];
    
//    cell.textLabel.text = [self.cityArray[indexPath.row] cityNameAbbr];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

//    实现协议方法
    [self.delegate passValueWithModel:self.self.dicc[self.smallArray[indexPath.section]][indexPath.row]];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

//每一个大区域的标识
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.smallArray[section];
}
//右边的索引
- (NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.smallArray;
}






@end
